<?php
/**
 * @author Tyil <info@tyil.nl>
 * @version 1.0 beta [30-03-2012]
 */

// Allow for sideloading other files safely
define('SIDELOADED', true);

// Get the base PATH for file inclusion, and the URL for redirection
define('PATH', $_SERVER['DOCUMENT_ROOT'] . str_replace('/index.php', '', $_SERVER['PHP_SELF']));
define('URL', 'http://' .  $_SERVER['SERVER_NAME'] . str_replace('/index.php', '', $_SERVER['PHP_SELF']));

// Require all configuration files
$dir = scandir(PATH . '/config/');

foreach($dir as $file) {
  
  if(!in_array($file, array('.', '..', '.svn', '.htaccess'))) {
    
    require PATH . '/config/' . $file;
    
  }
  
}

// Require all necessary files
require PATH . '/core/class/website.php';

// Set the environment variables
switch($config['website']['environment']) {
  
  case 'dev':
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    ini_set('track_errors', 1);
    ini_set('html_errors', 1);
    break;
  
  case 'prod':
  default:
    error_reporting(E_ALL & ~E_NOTICE);
    ini_set('display_errors', 'Off'); 
    break;
  
}

if(count(@$config['website']['autoload_classes']) > 0) {
  // There are classes to autoload
  
  foreach($config['website']['autoload_classes'] as $class) {
    
    if(file_exists(PATH . '/custom/class/' . $class . '.php')) {
      
      include PATH . '/custom/class/' . $class . '.php';
      
    } elseif(file_exists(PATH . '/core/class/' . $class . '.php')) {
      
      include PATH . '/core/class/' . $class . '.php';
      
    } else {
      
      user_error('Could not load autoloading class "' . $class . '"', E_USER_ERROR);
      
    }
    
  }
  
}

if(count(@$config['website']['autoload_functions']) > 0) {
  // There are functions to autoload
  
  foreach($config['website']['autoload_functions'] as $function) {
    
    if(file_exists(PATH . '/custom/function/' . $function . '/')) {
      // There's a directory of functions
      
      $files = scandir(PATH . '/custom/function/' . $function . '/');
      
      foreach($files as $file) {
        
        if(!in_array($file, array('.', '..', '.svn', '.htaccess'))) {
        
          include PATH . '/custom/function/' . $function . '/' . $file;
        
        }
        
      }
      
    } elseif(file_exists(PATH . '/custom/function/' . $function . '.php')) {
      
      include PATH . '/custom/function/' . $function . '.php';
      
    } elseif(file_exists(PATH . '/core/function/' . $function . '/')) {
      // There's a directory of core functions
      
      $files = scandir(PATH . '/core/function/' . $function . '/');
      
      foreach($files as $file) {
        
        if(!in_array($file, array('.', '..', '.svn', '.htaccess'))) {
        
          include PATH . '/core/function/' . $function . '/' . $file;
        
        }
        
      }
      
    } elseif(file_exists(PATH . '/core/function/' . $function . '.php')) {
      
      include PATH . '/core/function/' . $function . '.php';
      
    } else {
      
      user_error('Could not load autoload function "' . $function . '"', E_USER_ERROR);
      
    }
    
  }
  
}

// Call the router for execution
require PATH . '/core/router.php';
